package com.czl.module_service.viewmodel

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.AddSortBean
import com.czl.base.data.bean.tengyun.CategoryBean
import com.czl.base.data.bean.tengyun.ListBean
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.util.RxThreadHelper
import com.czl.base.util.SpHelper
import com.czl.module_service.extension.AbsHandleSubscriber
import com.czl.module_service.system.Constants
import io.reactivex.Observable

/**
 * @author by xubin,
 * @date on 2022/1/20.
 * PS: Not easy to write code, please indicate.
 */
class AddSortViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {
    val uc = UiChangeEvent()

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<SortBean> = SingleLiveEvent()
        val loadCategoryEvent: SingleLiveEvent<ListBean<CategoryBean>> = SingleLiveEvent()
        val selectedImageEvent: SingleLiveEvent<Any> = SingleLiveEvent()
    }

    val data: MutableList<AddSortBean> by lazy {
        mutableListOf(
            AddSortBean(leftTitle = "产权公司*", isEditable = false, isSelectable = true),
            AddSortBean(leftTitle = "所属分类*", isEditable = false, isSelectable = true),
            AddSortBean(leftTitle = "物品名称*", isEditable = false, isSelectable = true),
            AddSortBean(leftTitle = "物品编号*：", isEditable = false, isSelectable = true),
            AddSortBean(leftTitle = "规格型号*：", isEditable = false, isSelectable = true),
            AddSortBean(leftTitle = "单位*：", isEditable = false),
            AddSortBean(leftTitle = "管理方式*：", isEditable = false, isSelectable = true),
            AddSortBean(leftTitle = "使用方式*：", isEditable = false, isSelectable = true),
            AddSortBean(leftTitle = "计价方式*：", isEditable = false, isSelectable = true),
            AddSortBean(leftTitle = "图片：", isEditable = false, isSelectable = false),
//            AddSortBean(leftTitle = "最低库存*：", isEditable = true, isSelectable = false),
        )
    }

    var sortBean: SortBean? = null
        set(value) {
            field = value
            data[0].rightTitle = value?.companyName
            data[1].rightTitle = value?.classificationName
            data[2].rightTitle = value?.sortName
            data[3].rightTitle = value?.sortCode
            data[4].rightTitle = value?.sortModel
            data[5].rightTitle = value?.unit
            data[6].rightTitle = value?.manageModeStr
            data[7].rightTitle = value?.useModeStr
            data[8].rightTitle = value?.varuationStr

            if (value != null) {
                postSortBean = value
            }
        }

    var postSortBean: SortBean? = null
        get() {
            if (field == null) {
                return SortBean()
            }
            return field
        }

    /**
     * 提交数据
     */
    val submitCommand: BindingCommand<Void> = BindingCommand(BindingAction {
        submit()
    })

    var selectedImageUrl = ObservableField("")

    val imageClick: BindingCommand<Any> = BindingCommand(BindingAction {
        uc.selectedImageEvent.call()

    })

    fun getClassStockList(propCompanyId: Int?) {
        postSortBean?.let {
            doSubscribe(
                model.mTengYunHttpDataSource.getClassStockList(
                    GsonUtils.toJson(
                        mapOf(
                            "companyId" to SpHelper.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID),
                            "propCompanyId" to propCompanyId,
                            "classStatusList" to listOf(1, 2),
                            "storehouseIdList" to listOf(SpHelper.decodeInt(Constants.SpKey.STOREHOUSE_ID))
                        )
                    )
                ), object : AbsHandleSubscriber<BaseBean<ListBean<CategoryBean>>>() {
                    override fun onSuccess(t: BaseBean<ListBean<CategoryBean>>) {
                        dismissLoading()
                        uc.loadCategoryEvent.postValue(t.data)
                    }
                }
            )
        }
    }

    fun <T> doSubscribe(
        observable: Observable<BaseBean<T>>,
        observer: AbsHandleSubscriber<BaseBean<T>>,
        showLoading: Boolean = true
    ) {
        observable
            .doOnSubscribe {
                if (showLoading) {
                    showLoading()
                }
            }
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doFinally {
                if (showLoading) {
                    dismissLoading()
                }
            }
            .subscribe(observer)
    }

    private fun submit() {
        postSortBean?.let {
            doSubscribe(
                model.mTengYunHttpDataSource.addSortInfo(
                    GsonUtils.toJson(
                        it
                    )
                ), object : AbsHandleSubscriber<BaseBean<SortBean>>() {
                    override fun onSuccess(t: BaseBean<SortBean>) {
                        dismissLoading()
                        uc.loadCompleteEvent.postValue(t.data)
                    }
                })
        }
    }
}